package com.czk.netty.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/15 22:19
 */
public class NettyByteBuf02 {
    public static void main(String[] args) {
        // 创建ByteBuf
        ByteBuf buf = Unpooled.copiedBuffer("hello,world!", StandardCharsets.UTF_8);
        if (buf.hasArray()) {
            //
            byte[] content = buf.array();
            // 将其转成字符串
            System.out.println(new String(content, StandardCharsets.UTF_8));
            // width=12.capacity=36
            System.out.println("bytebuf" + buf);
            // 0
            System.out.println(buf.arrayOffset());
            // 0
            System.out.println(buf.readerIndex());
            // 12
            System.out.println(buf.writerIndex());
            // 12
            System.out.println(buf.capacity());
            // 可读取的字节数 12
            int i = buf.readableBytes();

            for (int j = 0; j <i; j++) {
                System.out.println((char)buf.readByte());
            }
            // 104 h对应的asi码
            System.out.println(buf.readByte());
            // 11
            System.out.println(buf.readableBytes());
        }
    }
}
